﻿Imports System.Windows.Forms.DataVisualization.Charting

Public Class fPrehledyDetailGraf
    Public checkPrijmy As Boolean = My.Forms.fPrehledyDetailZobrazeni.chboxPrijmy.Checked
    Public checkVydaje As Boolean = My.Forms.fPrehledyDetailZobrazeni.chboxVydaje.Checked



    Private Sub fPrehledyDetailGraf_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.Dispose()
    End Sub
    Private Sub fPrehledyDetailGraf_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        If checkPrijmy = True Then
            Dim tabSource As DataTable = Me.DtbDataSet1.tab_prijem 'set source table
            Me.Tab_prijemTableAdapter1.Fill(tabSource) 'and fill it
            Dim tabTemp As DataTable = Me.DtbDataSet1.forChart_Prijem 'set output table used for storing values for chart (prepped in dataset)

            'use public sub (in mod01.vb) to extract and group data to needed format
            GetSumsForChart(tabSource, tabTemp, "datum_prodeje", "cena_kus", "mnozstvi")

            Try
                Dim area As ChartArea = chartPrehled.ChartAreas.Item(0) 'set chart area
                chartPrehled.DataSource = tabTemp 'set data source
                'construct and fill series

                Dim sMnozstvi As New Series() 'create new series to plot and set values
                With sMnozstvi
                    .ChartArea = area.Name
                    .ChartType = SeriesChartType.Column
                    .LegendText = "Celkový příjem za den"
                    .IsVisibleInLegend = True
                    '.IsXValueIndexed = True
                    .XValueMember = tabTemp.Columns("datum").ColumnName
                    .XAxisType = AxisType.Primary
                    .XValueType = ChartValueType.Auto
                    .YValueMembers = tabTemp.Columns("celkPrijem").ColumnName
                    .YAxisType = AxisType.Primary
                    .YValueType = ChartValueType.Int64
                    .Enabled = True
                End With
                chartPrehled.Series.Add(sMnozstvi)

                'constructs chart axes
                Dim aX As New Axis()
                With aX
                    .Name = "X"
                    .ArrowStyle = AxisArrowStyle.None
                    .IntervalAutoMode = IntervalAutoMode.VariableCount
                    .IntervalType = DateTimeIntervalType.Days
                    .IsLabelAutoFit = True
                    .IsStartedFromZero = False
                    .Title = "Datum prodeje"
                    .Enabled = AxisEnabled.True
                End With
                area.AxisX = aX

                Dim aY As New Axis()
                With aY
                    .Name = "Y"
                    .ArrowStyle = AxisArrowStyle.None
                    .IntervalAutoMode = IntervalAutoMode.VariableCount
                    .IsLabelAutoFit = True
                    .IsStartedFromZero = False
                    .Title = "Celkový příjem za den"
                    .Enabled = AxisEnabled.True
                End With
                area.AxisY = aY

                chartPrehled.Update()
                chartPrehled.Show()

            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Chyba generování grafu příjmů!")
                Me.Close()
                Exit Sub
            End Try

        ElseIf checkVydaje = True Then
            'apply same comments as in section above
            Dim tabSource As DataTable = Me.DtbDataSet1.tab_vydaj
            Me.Tab_vydajTableAdapter1.Fill(tabSource)
            Dim tabTemp As DataTable = Me.DtbDataSet1.forChart_Prijem

            GetSumsForChart(tabSource, tabTemp, "datum_nakupu", "cena_kus", "mnozstvi")

            Try
                Dim area As ChartArea = chartPrehled.ChartAreas.Item(0)
                chartPrehled.DataSource = tabTemp
                'construct and fill series

                Dim sMnozstvi As New Series()
                With sMnozstvi
                    .ChartArea = area.Name
                    .ChartType = SeriesChartType.Column
                    .LegendText = "Celkový výdaj za den"
                    .IsVisibleInLegend = True
                    '.IsXValueIndexed = True
                    .XValueMember = tabTemp.Columns("datum").ColumnName
                    .XAxisType = AxisType.Primary
                    .XValueType = ChartValueType.Auto
                    .YValueMembers = tabTemp.Columns("celkPrijem").ColumnName
                    .YAxisType = AxisType.Primary
                    .YValueType = ChartValueType.Int64
                    .Enabled = True
                End With
                chartPrehled.Series.Add(sMnozstvi)

                'constructs chart axes
                Dim aX As New Axis()
                With aX
                    .Name = "X"
                    .ArrowStyle = AxisArrowStyle.None
                    .IntervalAutoMode = IntervalAutoMode.VariableCount
                    .IntervalType = DateTimeIntervalType.Days
                    .IsLabelAutoFit = True
                    .IsStartedFromZero = False
                    .Title = "Datum nákupu"
                    .Enabled = AxisEnabled.True
                End With
                area.AxisX = aX

                Dim aY As New Axis()
                With aY
                    .Name = "Y"
                    .ArrowStyle = AxisArrowStyle.None
                    .IntervalAutoMode = IntervalAutoMode.VariableCount
                    .IsLabelAutoFit = True
                    .IsStartedFromZero = False
                    .Title = "Celkový výdaj za den"
                    .Enabled = AxisEnabled.True
                End With
                area.AxisY = aY


                chartPrehled.Update()
                chartPrehled.Show()

            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Chyba generování grafu výdajů!")
                Me.Close()
                Exit Sub
            End Try

        Else
            MsgBox("Je třeba zvolit položku pro zobrazení!", MsgBoxStyle.Information, "Upozornění!")
            Me.Close()
            Exit Sub
        End If


    End Sub

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

End Class